package leetcode.day;

import java.util.*;

// 6. Z 字形变换
public class Convert {
    public static void main(String[] args) {

    }

    public String convert(String s, int numRows) {
        if (numRows < 2) {
            return s;
        }
        List<StringBuffer> row = new ArrayList<>();
        for (int i = 0; i < numRows; i++) {
            row.add(new StringBuffer());
        }
        int index = 0;
        int flag = -1; // 反转
        for (char c : s.toCharArray()) {
            row.get(index).append(c);
            if (index == 0 || index == numRows - 1) {
                // 反转
                flag = -flag;
            }
            index += flag;
        }
        StringBuffer res = new StringBuffer();
        for (int i = 0; i < numRows; i++) {
            res.append(row.get(i));
        }
        return res.toString();
    }
}
